
import {
  BOOK_UPDATE,
  BOOK_REMOVE,
  BOOK_SWITCH,
  BOOK_LIST_RESET,
} from '../actions/actionTypes';

let initialBookState = {
  items: [],
  current: undefined
};

export default function (state = initialBookState, action) {
    switch (action.type) {
      case BOOK_SWITCH:
        return Object.assign({}, state, {type: action.type, current: action.book});
      case BOOK_LIST_RESET:
        return Object.assign({}, state, {type: action.type, items: action.items});
      case BOOK_UPDATE:
        return Object.assign({}, state, {
          type: action.type,
          items: state.items.map((item)=> item._id == action.book._id ? action.book : item),
        });
      case BOOK_REMOVE: {
        return Object.assign({}, state, {
          type: action.type,
          items: state.items.filter((item)=> item._id != action._id),
        });
      }
    }
    return state;
  }